#include "portCtrl.h"


#define BEEP_GPIO		GPIO_NUM_13
#define MOTOR_GPIO		GPIO_NUM_4

PORTCtrlClassStruct PORTCtrlClass;

void vibration_run_task(void *pvParameters)
{
    
    while(1)
    {
        if(PORTCtrlClass.vibrationNum > 0)
        {
            ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 160);
            ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
            vTaskDelay(300 / portTICK_PERIOD_MS);
            ledc_set_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0, 0);
            ledc_update_duty(LEDC_HIGH_SPEED_MODE, LEDC_CHANNEL_0);
            vTaskDelay(700 / portTICK_PERIOD_MS);
            PORTCtrlClass.vibrationNum--;
        }
    }
}
void beep_run_task(void *pvParameters)
{

    while(1)
    {
        if(PORTCtrlClass.beepNum > 0)
        {
            gpio_set_level(BEEP_GPIO, 1);
            vTaskDelay(300 / portTICK_PERIOD_MS);
            gpio_set_level(BEEP_GPIO, 0);
            vTaskDelay(700 / portTICK_PERIOD_MS);
            PORTCtrlClass.beepNum--;
        }
    }
}
/**
  * @brief  LED初始化
  * @param  None
  * @retval None
  * @note   None
*/
void PORT_init(void)
{
    ledc_timer_config_t motor_timer = {
        .duty_resolution = LEDC_TIMER_8_BIT,
        .freq_hz = 100,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .timer_num = LEDC_TIMER_0,
        .clk_cfg = LEDC_AUTO_CLK
    };
    ledc_timer_config(&motor_timer);

    ledc_channel_config_t motor_channel = {
        .channel = LEDC_CHANNEL_0,
        .duty = 0,
        .gpio_num = MOTOR_GPIO,
        .speed_mode = LEDC_HIGH_SPEED_MODE,
        .hpoint = 0,
        .timer_sel = LEDC_TIMER_0,
        .intr_type = LEDC_INTR_DISABLE
    };
    ledc_channel_config(&motor_channel);

	gpio_config_t gpio_config_struct;

	gpio_config_struct.pin_bit_mask = (1ULL << BEEP_GPIO);
	gpio_config_struct.mode = GPIO_MODE_OUTPUT;
	gpio_config_struct.pull_up_en = 0;
	gpio_config_struct.pull_down_en = 0;
	gpio_config_struct.intr_type = GPIO_PIN_INTR_DISABLE;

	gpio_config(&gpio_config_struct);

    gpio_set_level(BEEP_GPIO, 0);

    xTaskCreate(vibration_run_task, "vibration_run_task", 1024, NULL, 0, NULL);
    xTaskCreate(beep_run_task, "beep_run_task", 1024, NULL, 0, NULL);
}

